/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.Timer;

/**
 * This panels displays an animation to indicate the user a loading process.
 *
 */
public class LoadingPanel extends JComponent implements ActionListener {
	private static final long serialVersionUID = -2111078641602854875L;
	
	private Timer timer;
	
	private int xPos = 64;
	private int yPos = 0;
	
	private ImageIcon inputImage;
	private BufferedImage sourceBI = null;
	
	public LoadingPanel() {
		inputImage = new ImageIcon(
				getClass().getResource("images/loading.png"));
		sourceBI = new BufferedImage(inputImage.getIconWidth(),
				inputImage.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = sourceBI.createGraphics();
		g.drawImage(inputImage.getImage(), 0, 0, this);
		
		timer = new Timer(30, this);
		timer.setInitialDelay(10);
		timer.start();
		
		setPreferredSize(new Dimension(64, 64));
		setOpaque(false);
	}
	
	/**
	 * Starts the timer with animation.
	 */
	public void startAnimation() {
		setAnimationStarted(true);
	}
	
	/**
	 * Stops the timer with animation.
	 */
	public void stopAnimation() {
		setAnimationStarted(false);
	}
	
	public void setAnimationStarted(boolean b) {
		if(b) {
			timer.start();
		} else {
			xPos = 64;
			yPos = 0;
			timer.stop();
		}
	}
	
	public boolean isAnimationStarted() {
		return timer.isRunning();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		  
		int x = (getWidth() - 32)/2;
		int y = (getHeight() - 32)/2;
		g2d.drawImage(sourceBI.getSubimage(xPos, yPos, 32, 32), x, y, this);
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if (xPos+32 >= sourceBI.getWidth()) {
			xPos = 0;
			if (yPos+32 >= sourceBI.getHeight()) {
				yPos = 0;
				xPos = 64;
			} else {
				yPos += 32;
			}
		} else {
			xPos += 32;
		}
		
		repaint();
	}
}